<?php
namespace App\Http\Controllers\Admin;

use App\Services\Admin\AdService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class AdController extends BaseController
{
    public function __construct(AdService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getIndex(Request $request)
    {
        $pages['start'] = $request->get('pageStart', 0);
        $pages['limit'] = $request->get('pageLimit', 10);
        $rst = $this->db->getIndex($pages);
        if ($rst['code'] != 0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson(0, '', $rst['data']);
    }

    /**
     * 新增
     */
    public function modify(Request $request)
    {;
        $id = $request->post('id', 0);
        $pas['uid'] = $request->post('uid', 0);
        $thumb = $request->post('thumb', '');
        $pas['total'] = $request->post('total', 0);
        if ($thumb && substr($thumb, 0, 5) == 'data:') {
            $pas['thumb'] = Upload::getPathByBase64($thumb);
        }
        if ($id) {
            $res = $this->db->modify($id, $pas);
        } else {
            $res = $this->db->add($pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 开启|关闭
     */
    public function setRun(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->setRun($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    /**
     * 设置删除
     */
    public function setDel(Request $request)
    {;
        $id = $request->post('id', 0);
        $res = $this->db->setDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //已展示列表
    public function usedList(Request $request)
    {
        $id = $request->get('id', 0);
        $pages['start'] = $request->get('pageStart', 0);
        $pages['limit'] = $request->get('pageLimit', 10);
        $res = $this->db->usedList($pages, $id);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }
}